<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>


        <el-drawer
                title="数据字典明细"
                :visible.sync="drawer"
                :direction="direction"
                :before-close="handleClose">
            <dictionarydetail></dictionarydetail>
        </el-drawer>

		<!--列表-->
		<el-table :data="dictionarytypes" @row-dblclick="showDetals" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
            <el-table-column prop="name" label="产品名字" width="300" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="产品编号" width="200" sortable>
			</el-table-column>


    <el-table-column label="操作" >
        <template scope="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
        </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar"><!--如果没有选中就不能使用:disabled="this.sels.length===0"-->
        <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next"
                       @current-change="handleCurrentChange"
                       :page-size="pageSize"
                       :total="total"
                       :current-page="currentPage"
                       style="float:right;">
        </el-pagination>
        </el-col>


        <!--新增/编辑界面-->
        <!-- :visible.sync  控制dialog是否显示
         :rules  绑定表单校验规则
         ref="saveForm"  给当前表单取一个别名
         prop:绑定校验规则的名称
         -->
        <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
        <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="产品编号" prop="sn">
            <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品名称" prop="name">
            <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
        </div>
        </el-dialog>
        </section>
        </template>

        <script>
        import dictionarydetail from './Dictionarydetail.vue'

    export default {
        data() {
            return {
                title:"",
                filters: {
                    keyword: ''
                },
                dictionarytypes:[],//数据字典
                total: 0,
                drawer:false,
                currentPage: 1,
                pageSize:10,
                listLoading: false,
                sels: [],//列表选中行

                saveFormVisible: false,//新增界面是否显示
                saveLoading: false,
                saveFormRules: {
                    sn: [//绑定表单校验规则
                        { required: true, message: '请输入产品编号', trigger: 'blur' }
                    ],
                    name: [
                        { required: true, message: '请输入产品姓名', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                saveForm: {
                    id:null,
                    sn: "",
                    name: ""
                }

            }
        },
        methods: {
            showDetals(){
                this.drawer = true;
            },
            search(){//高级查询完定位到第一页
                this.currentPage=1;
                this.getDictionarytypes();
            },
            handleCurrentChange(val) {
                this.currentPage = val;//点击页面给模型层赋值
                this.getDictionarytypes();
            },
            //获取员工列表
            getDictionarytypes() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true;//开启忙等框
                //NProgress.start();
                //发送axios请求从后端拿取数据
                this.$http.post("/sdt/page",para)
                    .then(result=>{
                        this.listLoading = false;//关闭忙等框
                        console.log(result);
                        result=result.data;
                        this.dictionarytypes=result.rows;
                        this.total=result.total;
                    })
                    .catch(result=>{
                        console.log(result);
                        this.$message({
                            type:'error',
                            message:'系统繁忙稍后重试'
                        })
                    })
            },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {//点击确定按钮
					this.listLoading = true;

					this.$http.delete("/sdt/"+row.id)
                        .then(result=>{
                            this.listLoading = false;
                            result=result.data;//AjaxResult success:true/false
                            if(result.success){//删除成功
                                this.$message({
                                    type:'success',
                                    message:result.message
                                });
                                this.currentPage=1;//删除操作后定义到第一页
                                this.getDictionarytypes();
                            }else {//删除失败
                                this.$message({
                                    type:'error',
                                    message:result.message
                                });
                                this.currentPage=1;//删除操作后定义到第一页
                                this.getDictionarytypes();
                            }
                        })
                        .catch(result=>{
                            this.listLoading = false;
                            console.log(result);
                            this.$message({
                                type:'error',
                                message:'系统繁忙稍后重试'
                            })
                        })
                }).catch(() => {//点击取消按钮

                });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
                this.title="编辑";
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
                this.title="新增";
				this.saveFormVisible = true;
				this.saveForm = {//表格初始化清空
                    id:null,
                    sn: "",
                    name: ""
				};
			},
			//编辑
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {//提交表单校验
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {})
                            .then(() => {//点击确定提交
							this.saveLoading = true;//开启忙等框
                            //NProgress.start();
                            let para = Object.assign({}, this.saveForm);
                            //处理数据格式
                            this.$http.post("/sdt/save",para)
                                    .then(result=>{
                                        this.saveLoading=false;//关闭忙等框
                                        result=result.data;
                                        if(result.success){//删除成功
                                            this.$message({
                                                type:'success',
                                                message:result.message
                                            });
                                            //保存成功，页面加载到第一页
                                            this.currentPage=1;
                                            this.getDictionarytypes();
                                            //重新刷新表单校验
                                            this.$refs['saveForm'].resetFields();
                                            this.saveFormVisible=false//关闭模态框
                                        }else {
                                            this.$message({
                                                type:'error',
                                                message:result.message
                                            });
                                            //保存成功，页面加载到第一页
                                            this.currentPage=1;
                                            this.getDictionarytypes();
                                        }
                                    })
                                .catch(result=>{
                                    console.log(result);
                                    this.$message({
                                        type:'error',
                                        message:"系统繁忙，和程序员沟通中"
                                    })
                                })

						});
					}
				});
			},
			selsChange: function (sels) {//复选框选中时，触发。选中复选框选中的所有行数据
				this.sels = sels;
                //console.log(this.sels)
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)//.toString();[数据]
				//console.log(ids)
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();

                    this.$http.patch("/emp",ids)
                        .then(result=>{
                            this.listLoading = false;
                            result=result.data;//AjaxResult success:true/false
                            if(result.success){//删除成功
                                this.$message({
                                    type:'success',
                                    message:result.message
                                });
                                this.currentPage=1;//删除操作后定义到第一页
                                this.getDictionarytypes();
                            }else {//删除失败
                                this.$message({
                                    type:'error',
                                    message:result.message
                                });
                                this.currentPage=1;//删除操作后定义到第一页
                                this.getDictionarytypes();
                            }
                        })
                        .catch(result=>{
                            this.listLoading = false;
                            console.log(result);
                            this.$message({
                                type:'error',
                                message:'系统繁忙稍后重试'
                            })
                        })

				}).catch(() => {

				});
			},
		},
		mounted() {
			this.getDictionarytypes();
		},
        components:{
            dictionarydetail
        }
	}

</script>

<style scoped>

</style>